﻿using Autofac;
using Common;
using DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZoneLine.Common;
using ZoneLine.Models;
using ZoneLine.Models.PrintRecord;

namespace ZoneLine.Printers
{
    public class BQKHPrinter : IPrinter
    {
        public string PrintLabel(string path, string printerName, int startIndex, int? endIndex = null)
        {
            var (paras, code) = GetPara(startIndex);
            PrinterHelper.PrinterLabel(path, printerName, paras);
            return code;
        }

        public void PrintLabelShow(string printTemplatePath, string printerName, int startIndex, int? endIndex = null)
        {
            var (paras, code) = GetPara(startIndex);

            PrinterHelper.PrinterLabelShow(printTemplatePath, printerName, paras);
        }

        private (Dictionary<string, string>, string) GetPara(int index)
        {
            var config = ContainerServer.Container.Resolve<PrintConfigTotal>();
            var lxConfig = config.BqqcKhConfig;
            var paras = new Dictionary<string, string>();
            paras.Add("LJH_N4", lxConfig.PartNumber);
            paras.Add("GYSDM_N4", lxConfig.SupplierCode);
            paras.Add("GKBJ_N4", lxConfig.ControlFlag);
            paras.Add("QDFS_N4", lxConfig.DriveMode);

            var batchCode = GetBatchCode(index);
            paras.Add("PCBM_N4", batchCode);

            var code = $"{lxConfig.SupplierCode}{batchCode}K{lxConfig.PartNumber}";
            paras.Add("EWM_N4", code);
            var para = StationEnableConfig.GetPara();
            if (Cache.WorkDirection == WorkDirectionEnum.Left)
            {
                paras.Add("QHZY_N4", "L");
            }
            else
            {
                paras.Add("QHZY_N4", "R");
            }
            BQKHRecord bQKHRecord = new BQKHRecord();
            bQKHRecord.SerialNumber = index;
            bQKHRecord.Code = code;
            bQKHRecord.BatchNumber = lxConfig.BatchNumber;
            bQKHRecord.ControlFlag = lxConfig.ControlFlag;
            bQKHRecord.Direction = lxConfig.Direction;
            bQKHRecord.DriveMode = lxConfig.DriveMode;
            bQKHRecord.PartNumber = lxConfig.PartNumber;
            bQKHRecord.PlatformCode = lxConfig.PlatformCode;
            bQKHRecord.SupplierCode = lxConfig.SupplierCode;
            bQKHRecord.TemplatePath = lxConfig.TemplatePath;
            bQKHRecord.TemplatePathR = lxConfig.TemplatePathR;
            DBContext.DbConfig.DbContext.Insert<BQKHRecord>(bQKHRecord).ExecuteInserted();
            return (paras, code);
        }

        private string GetBatchCode(int index)
        {
            var years = DateTime.Now.Year.ToString().Substring(2);
            var mouths = DateTime.Now.Month.ToString("00");
            var days = DateTime.Now.Day.ToString("00");
            var indexs = index.ToString("00000");
            return $"{years}{mouths}{days}{indexs}";
        }
    }
}